﻿using System;
using System.Web.UI.WebControls;
using BLL;
using Model;
using ZedGraph;
using System.Collections.Generic;

public partial class StatisticData_PackageandUnion_PackageQbyShift : System.Web.UI.Page
{
    private DateTime var_starttime
    {
        get { return DateTime.Parse(ViewState["starttime"].ToString()); }
        set { ViewState["starttime"] = value; }
    }
    private DateTime var_stoptime
    {
        get { return DateTime.Parse(ViewState["stoptime"].ToString()); }
        set { ViewState["stoptime"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDTBetween();
        }
    }

    protected void BindDTBetween()
    {
        this.DropDownList1.DataSource = DayandNightShift.list;
        this.DropDownList1.DataTextField = "steptext";
        this.DropDownList1.DataValueField = "stepvalue";
        this.DropDownList1.DataBind();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        BindPackageQ();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string day = this.TextBox1.Text.Trim();
        string[] dtbetween = this.DropDownList1.SelectedValue.Split(',');
        var_starttime = DateTime.Parse(day + dtbetween[0]);
        var_stoptime = DateTime.Parse(day + dtbetween[1]);
        if (var_starttime > var_stoptime)
        {
            var_stoptime = var_stoptime.AddDays(1);
        }
        BindPackageQ();
        this.ZedGraphWeb1.Visible = true;
    }

    protected void BindPackageQ()
    {        
        //switch (this.DropDownList1.SelectedIndex)
        //{
        //    case 1:
        //        var_starttime = DateTime.Parse(day + " 08:00:00");
        //        var_stoptime = DateTime.Parse(day + " 20:00:00");
        //        break;
        //    case 2:
        //        var_starttime = DateTime.Parse(day + " 20:30:00");
        //        var_stoptime = DateTime.Parse(day + " 07:30:00").AddDays(1);
        //        break;
        //    default:
        //        var_starttime = DateTime.Parse(day + " 07:30:00");
        //        var_stoptime = DateTime.Parse(day + " 20:30:00");
        //        break;
        //}
        PackageProduct test = new PackageProduct();
        GridView1.DataSource = test.GetPackagedQbyAllPIDDT(var_starttime, var_stoptime);
        GridView1.DataBind();
    }

    protected void ZedGraphWeb1_RenderGraph(ZedGraph.Web.ZedGraphWeb webObject, System.Drawing.Graphics g, ZedGraph.MasterPane pane)
    {
        int i = 0;
        double total = 0;
        PackageProduct tqt = new PackageProduct();
        List<Time_QInfo> t_qs = (List<Time_QInfo>)tqt.GetPackagedQbyAllPIDDT(var_starttime, var_stoptime);

        GraphPane myPane = pane[0];

        myPane.Title.Text = "各产品类型包装量统计";

        //创建曲线
        double[] list = new double[t_qs.Count];
        string[] labels = new string[t_qs.Count];
        foreach (Time_QInfo t_q in t_qs)
        {
            list[i] = (double)t_q.Quantity;
            labels[i] = t_q.DayTime;
            i++;
        }

        for (int j = 0; j < t_qs.Count; j++)
        {
            total += list[j];
        }

        int count = 0;
        double other = 0;
        for (int j = 0; j < t_qs.Count; j++)
        {
            if (list[j] / total >= 0.05)
            {
                count++;
            }
            else
                other += list[j];
        }
        double[] list1 = new double[count + 1];
        string[] labels1 = new string[count + 1];
        int s = 0;
        for (int j = 0; j < t_qs.Count; j++)
        {
            if (list[j] / total >= 0.05)
            {
                list1[s] = list[j];
                labels1[s] = labels[j];
                s++;
            }
        }
        list1[count] = other;
        labels1[count] = "其它";

        PieItem[] pi =myPane.AddPieSlices(list1, labels1);
        foreach (PieItem p in pi)
        {
            MyGraphStyle.BuildPieItem(p);
            p.LabelDetail.FontSpec.Size = 10f;
        }

        MyGraphStyle.BuildpieGraphStyle(myPane);
        pane.AxisChange(g);
    }
}
